package es.uji.viselab.benchmark.process;

import java.awt.FlowLayout;
import java.awt.TextArea;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;

import es.uji.viselab.benchmark.DataType;
import es.uji.viselab.experiment.ExperimentParameters;
import es.uji.viselab.math.Matrix4;

public class StatsProcessor extends BenchMarkingProcessor {

	TextArea results;

	@Override
	public void run() {

		int groupId = dbConnection.getGroupId();
		List<Integer> experimentsList = dbConnection.getExperimentsList(groupId);
		Iterator<Integer> iterator = experimentsList.iterator();
		while (iterator.hasNext()) {
			int experimentId = iterator.next();
			ExperimentParameters ep = dbConnection.getExperimentParameters(experimentId);
			add("Experiment:" +experimentId);
			add(ep.getVSMethod().toString());
			add(ep.getCamParameters().toString());
			add(ep.getExperiment().toString());
			add("Target:");
			Matrix4 poseTarget = ep.getTarget();
			add(poseTarget.toStringXYZ());
			double finalError = dbConnection.getIndicator(experimentId,DataType.FINAL_ERROR);
			double totalSteps = dbConnection.getIndicator(experimentId,DataType.TOTAL_STEPS);
			add("error " + finalError + " reached at " + totalSteps + " steps");
		}
	}

	private void add(String s) {
		results.setText(results.getText() + "\n" + s);

	}

	@Override
	protected void createContent() {
		super.createContent();

		results = new TextArea(10, 40);
		JPanel line1 = new JPanel();
		line1.setLayout(new FlowLayout());
		line1.add(results);

		rootPanel.add(line1);

	}

	@Override
	public String getPanelTitle() {
		return "General Stats";
	}

	@Override
	public int panelWidth() {
		return 500;
	}

	@Override
	public int panelHeight() {
		return 300;
	}

}
